<template>
  <div class="headerbox">
    <div class="header">
      <!-- 导航 -->
      <van-nav-bar
        :title="ProductInfo.name"
        left-text=""
        left-arrow
        @click-left="back"
        @click-right="ShowShare = true"
      >
        <template #right>
          <van-icon name="ellipsis" color="#1989fa" />
        </template>
      </van-nav-bar>
      <!-- 分享面板 -->
      <van-share-sheet
        v-model:show="ShowShare"
        title="立即分享给好友"
        :options="options"
        @select="onSelect"
      />
    </div>
  </div>
  <div class="clear"></div>
  <div class="h50"></div>
  <div class="clear"></div>
  <!-- 轮播 -->
  <div class="aui-m-slider">
    <van-swipe :autoplay="3000">
      <van-swipe-item
        v-for="(item, index) in ProductInfo.thumbs_cdn"
        :key="item"
      >
        <img :src="item" />
      </van-swipe-item>
    </van-swipe>
  </div>
  <div class="clear"></div>

  <div class="detailbox">
    <h4>{{ ProductInfo.name }}</h4>
    <div class="price" v-if="ProductInfo.level">
      <em>￥</em>{{ ProductInfo.level }}
      <span>￥{{ ProductInfo.price }}</span>
      <div class="stock">库存：{{ ProductInfo.stock }}件</div>
    </div>
    <div class="price" v-else>
      <em>￥</em>{{ ProductInfo.price }}
      <div class="stock">库存：{{ ProductInfo.stock }}件</div>
    </div>
  </div>
  <div class="clear"></div>
  <div class="allkbox"></div>
  <div class="clear"></div>
  <div class="detailbox_2">
    <van-tabs v-model:active="active" type="card">
      <van-tab title="产品参数">
        <div style="margin-top: 10px" v-html="ProductInfo.content"></div>
      </van-tab>
      <van-tab title="评价">
        <div class="rate-header">
          <div class="rate-score">
            <h4>与描述相符</h4>
            <strong>{{ ProductInfo.average }}</strong>
          </div>
          <div class="rate-graph">
            <van-rate v-model="ProductInfo.average" readonly allow-half />
          </div>
        </div>
        <div class="rate-content">
          <div v-if="ratelist.length > 0">
            <div class="review-box" v-for="item in ratelist" :key="item.id">
              <div class="review-item">
                <div class="from-whom">
                  <div class="avatar">
                    <img :src="item.user.avatar_cdn" alt="" />
                  </div>
                  <div class="name">{{ item.user.nickname }}</div>
                </div>
                <div class="review-details">
                  <div class="content">{{ item.comment }}</div>
                  <div style="margin-top: 10px">
                    <van-image
                      v-for="(items, index) in item.proimg_cdn"
                      :key="index"
                      width="4rem"
                      style="margin-right: 1rem"
                      height="4rem"
                      fit="cover"
                      :src="items"
                      @click="changeImg(items)"
                    />
                  </div>
                   <div class="time"> {{ item.updatetime }} </div>
                </div>
              </div>
            </div>
          </div>
          <div v-else>
            <div class="rate-empty">该商品还没有人写评价呢</div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
  <div class="clear"></div>
  <van-action-bar>
    <van-action-bar-icon icon="chat-o" text="客服" @click="contact" />
    <van-action-bar-icon
      icon="cart-o"
      text="购物车"
      @click="toPageCart"
      :badge="ProductInfo.cartcount"
    />
    <van-action-bar-button type="warning" text="加入购物车" @click="AddCart" />
    <van-action-bar-button type="danger" text="立即购买" @click="Addconfirm" />
  </van-action-bar>
</template>
<script>
import { ImagePreview } from "vant";
export default {
  emits: ["toggle"],
  created() {
    this.$emit("toggle", false);

    this.productDeatil();
  },
  data() {
    return {
      ShowShare: false,
      options: [
        [
          { name: "微信", icon: "wechat" },
          { name: "朋友圈", icon: "wechat-moments" },
          { name: "微博", icon: "weibo" },
          { name: "QQ", icon: "qq" },
        ],
        [
          { name: "复制链接", icon: "link" },
          { name: "分享海报", icon: "poster" },
          { name: "二维码", icon: "qrcode" },
          { name: "小程序码", icon: "weapp-qrcode" },
        ],
      ],
      Recommend: [],
      html: "",
      proid: this.$route.query.proid ? this.$route.query.proid : 0,
      LoginUser: this.$cookies.get("LoginUser")
        ? this.$cookies.get("LoginUser")
        : {},
      ProductInfo: {},
      active: 0,
      ratelist: [],
    };
  },
  methods: {
    // 返回上一级
    back() {
      this.$router.go(-1);
    },
    changeImg(val) {
      ImagePreview([val]);
    },
    async productDeatil() {
      let data = {
        proid: this.proid,
      };

      if (JSON.stringify(this.LoginUser) !== "{}") {
        data.userid = this.LoginUser.id;
      }

      var result = await this.$api.productDeatil(data);

      if (result.code == 1) {
        this.ProductInfo = result.data.product;
        this.ratelist = result.data.ratelist;
      }
    },
    toPageCart() {
      this.$router.push("/product/cart/index");
    },
    contact() {
      this.$dialog
        .confirm({
          title: "拨打电话",
          message: "是否确认拨打官方客服热线",
        })
        .then(() => {
          location.href = `tel:13512633441`;
          return;
        })
        .catch(() => {});
    },
    async AddCart() {
      if (!this.LoginUser || JSON.stringify(this.LoginUser) == "{}") {
        //提醒信息
        this.$notify({
          type: "danger",
          message: "请先登录",
          duration: 1000,
          onClose: () => {
            this.$router.push("/user/base/login");
            return false;
          },
        });
        return false;
      }

      var data = {
        userid: this.LoginUser.id,
        proid: this.proid,
      };

      var result = await this.$api.cartAdd(data);

      if (result.code == 1) {
        this.$dialog
          .confirm({
            message: result.msg,
          })
          .then(async () => {
            this.$router.push(result.url);
          })
          .catch(() => {});
      } else {
        this.$notify({
          message: result.msg,
          duration: 1000,
        });

        return false;
      }
    },
    Addconfirm() {
      if (!this.LoginUser || JSON.stringify(this.LoginUser) == "{}") {
        //提醒信息
        this.$notify({
          type: "danger",
          message: "请先登录",
          duration: 1000,
          onClose: () => {
            this.$router.push("/user/base/login");
            return false;
          },
        });
        return false;
      }
      this.$router.push({
        path: "/product/cart/confirm",
        query: {
          singcartids: this.proid,
        },
      });
    },
  },
};
</script>

<style lang="scss">
.detailbox_2 {
  .van-tabs__nav--card {
    margin: 0;
    border: 0;
  }

  .van-tabs__nav {
    background-color: #e5e5e5;
  }

  .van-tab {
    font-size: 12px;
    color: #333;
  }
}

.rate-header {
  padding: 10px 0;
  display: inline-flex;
  border-bottom: 1px solid #c8c8c8;

  .rate-score {
    width: 75px;
    padding: 0 20px;
    border-right: 1px dotted #ddd;
    text-align: center;

    h4 {
      color: #404040;
      font-size: 12px;
      font-weight: 100;
    }

    strong {
      color: #f60;
      font-family: arial;
      font-size: 32px;
      line-height: 32px;
      margin: 0 2px;
    }

    p {
      padding-top: 1px;
      padding-left: 7px;
      text-align: left;
    }
  }

  .rate-graph {
    padding-left: 90px;
    padding-top: 22px;
    width: 450px;
  }
}

.rate-content {
  position: relative;
  padding: 10px 0;
  margin-bottom: 40px;

  .review-box {
    width: 100%;
    padding: 10px 0;
    // justify-content:space-between;
    border-bottom: 1px solid #e3e3e3;
  }
  .review-item {
    display: inline-flex;
    align-items: center;

    .from-whom {
      width: 90px;
      text-align: center;
      line-height: 1.5;
      min-width: 90px;

      .avatar {
        width: 50px;
        height: 50px;
        display: inline-flex;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }

    .review-details {
      box-sizing: border-box;
      overflow: hidden;
      position: relative;
      zoom: 1;
      padding-right: 10px;

      .content {
        font-size: 14px;
        line-height: 1.4;
        color: #3f3f3f;
        word-wrap: break-word;
      }

      .time {
        font-size: 12px;
        color: #b0b0b0;
      }
    }
  }

  .rate-empty {
    text-align: center;
    height: 55px;
    line-height: 50px;
    color: #666;
  }
}
</style>